<?php
include "include/config.inc.php";
if(!isset($_SESSION['s_activId']))
{
  header('location:checkLogin.php');
}

$today = date("d/m/y");
$loanArray = array();
$i=0;

$selectLoan = "SELECT loan.loanId, loan.addOnLoanId ,party.partyName, item.itemId, loan.value, loan.pendingAmount , loan.loanAmount, 
                      item.itemName, loan.weight, loan.itemImage, itemlocation.itemlocationName, loan.loanDate, monthlyInterest, 
                      monthlyInterestRs, loanVarNo
                 FROM loan
            LEFT JOIN party ON loan.partyId = party.partyId
            LEFT JOIN itemlocation ON loan.itemlocationId = itemlocation.itemlocationId
            LEFT JOIN item ON loan.itemId = item.itemId
                WHERE isSold = 'N'
                  AND isDeleted = 'N'
             ORDER BY loanId DESC";
$selectLoanRes = mysql_query($selectLoan);
while($loanRow = mysql_fetch_array($selectLoanRes))
{
  $selectLoanAmount = "SELECT SUM(pendingAmount) AS totaLoanAmount
                         FROM loan
                        WHERE 1 = 1 ";
  if($loanRow['addOnLoanId'] != "")
  {
    $selectLoanAmount .= " AND loanId = ".$loanRow['addOnLoanId']."
                           OR addOnLoanId = ".$loanRow['addOnLoanId'];
  }
  else
  {
    $selectLoanAmount .= " AND loanId = ".$loanRow['loanId']."
                           OR addOnLoanId = ".$loanRow['loanId']." ";
  }
  $selectLoanAmountRes = mysql_query($selectLoanAmount);
  if($selectLoanAmountRow = mysql_fetch_array($selectLoanAmountRes))
  {
  	$loanArray[$i]['totalLoanOnThisItem'] = $selectLoanAmountRow['totaLoanAmount'];
  }
  else
  {
  	$loanArray[$i]['totalLoanOnThisItem'] = 0;
  }
  $currentInterestInRs                  = ($loanRow['pendingAmount'] * $loanRow['monthlyInterestRs'])/$loanRow['loanAmount']; 
  $loanArray[$i]['loanId']              = $loanRow['loanId'];
  $loanArray[$i]['addOnLoanId']         = $loanRow['addOnLoanId'];
  $loanArray[$i]['partyName']           = $loanRow['partyName'];
  $loanArray[$i]['value']               = $loanRow['value'];
  $loanArray[$i]['loanAmount']          = $loanRow['pendingAmount'];
  $loanArray[$i]['itemName']            = $loanRow['itemName'];
  $loanArray[$i]['weight']              = $loanRow['weight'];
  $loanArray[$i]['photo']               = $loanRow['itemImage'];
  $loanArray[$i]['itemlocationName']    = $loanRow['itemlocationName'];
  $loanArray[$i]['loanDate']            = date('d-m-Y',strtotime($loanRow['loanDate']));
  $loanArray[$i]['monthlyInterest']     = $loanRow['monthlyInterest'];
  $loanArray[$i]['monthlyInterestRs']   = $currentInterestInRs;
  $loanArray[$i]['loanVarNo']           = $loanRow['loanVarNo'];
  $i++;
}
include("bottom.php");
$smarty->assign('loanArray',$loanArray);  
$smarty->display('loanList.tpl');  
?>